﻿using MediatR;
using Tb.App.API.Applications.Command.Basic;
using Tb.App.API.Dtos.Rabbit;
using Tb.App.Domain.Rabbit.Basic;
using Tb.App.ErrorCode;
using Tb.App.Infrastructure.Interfaces;

namespace Tb.App.API.Applications.CommandHandler.Basic
{
    public class GetBasicQueryCommandHandler : IRequestHandler<GetBasicQueryCommand, ApiResult<ApiPaging<List<BasicDTO>>>>
    {
       
            private readonly IBaseRepository<BasicModel> basicRep;
            private readonly IBaseRepository<StoreModel> storeRep;

            public GetBasicQueryCommandHandler(IBaseRepository<BasicModel> basicRep, IBaseRepository<StoreModel> storeRep)
            {
                this.basicRep = basicRep;
                this.storeRep = storeRep;
            }
            public Task<ApiResult<ApiPaging<List<BasicDTO>>>> Handle(GetBasicQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<List<BasicDTO>>> result = new ApiResult<ApiPaging<List<BasicDTO>>>();
            result.Code = ApiEnum.查询成功;
            result.Message = ApiEnum.查询成功.ToString();

            var list = from a in basicRep.GetAll()
                       join b in storeRep.GetAll()
                       on a.Sid equals b.Id
                       where !a.IsDel
                       select new BasicDTO
                       {
                           Id = a.Id,
                           Sid = a.Sid,
                           StoreName = b.StoreName,
                           Batch = a.Batch,
                           Expiration = a.Expiration,
                           License = a.License,
                           Company = a.Company,
                           Picture = a.Picture,
                           Completeness = a.Completeness,
                           IsDel = a.IsDel,
                           Creator = a.Creator,
                           CreatorDate= a.CreatorDate,
                           Updator=a.Updator,
                           UpdatorDate= a.UpdatorDate,
                       };
            //查询
            if(!string.IsNullOrEmpty(request.StoreName))
            {
                list = list.Where(x => x.StoreName != null && x.StoreName.Contains(request.StoreName));
            }
            if (!string.IsNullOrEmpty(request.Batch))
            {
                list = list.Where(x => x.Batch != null && x.Batch.Contains(request.Batch));
            }
            if (request.Expiration > 0)
            {
                list = list.Where(x => x.Expiration == request.Expiration);
            }
            if (!string.IsNullOrEmpty(request.License))
            {
                list = list.Where(x => x.License != null && x.License.Contains(request.License));
            }
            if (!string.IsNullOrEmpty(request.Company))
            {
                list = list.Where(x => x.Company != null && x.Company.Contains(request.Company));
            }
            //分页
            ApiPaging<List<BasicDTO>> paging = new ApiPaging<List<BasicDTO>>();
            paging.TotalCount = list.Count();
            paging.PageCount = (int)Math.Ceiling((double)paging.TotalCount / request.PageSize);
            list = list.OrderByDescending(x => x.CreatorDate).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            paging.Data = list.ToList();

            result.Data = paging;
            return Task.FromResult(result);
        }
    }
}
